<?php
// +---------------------------------------------------------------------+
// | ALINEJUN    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/ALINEJUN                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\common\model\City;

/**
 * 前端首页控制器
 */
class Index extends IndexBase
{
    
    // 首页
    public function index($cid = 0)
    {
        $city_id = session('city_id');
        $cate_id = input('cate_id') ? input('cate_id') : 0;
        //先找出该城市下所有的城市
        $city_id and  $city_ids = $this->logicCity->getCityList(['pid'=>$city_id])->toArray();
        $city_ids = array_column($city_ids,'id');
        array_push($city_ids,(int)$city_id);
        //该城市下面的所有子城市都包括
        $where['city_id'] = ['in',$city_ids];
        $where['is_index'] = 1;
        $list =  $this->logicScenery->getSceneryList($where)->toArray();
        $cityModel = new City();
        foreach ($list['data'] as &$item) {
            $item['city_name'] = $cityModel->getAllName($item['city_id']);
            $item['map_name'] =  $cityModel->getName($item['city_id']);
        }
        $this->assign('list',$list);
        return $this->fetch('index');
    }
    
    // 详情
    public function details($id = 0)
    {
        
        $where = [];
        
        !empty((int)$id) && $where['a.id'] = $id;
        
        $data = $this->logicArticle->getArticleInfo($where);
        
        $this->assign('article_info', $data);
        
        $this->assign('category_list', $this->logicArticle->getArticleCategoryList([], true, 'create_time asc', false));
        
        return $this->fetch('details');
    }

    public function test()
    {
        $data = input('get.');
        $this->assign('city_name', '龙泉驿');
        $this->view->engine->layout(false);
        return $this->fetch('/map/test');
    }

    public function addMsg()
    {
        return $this->fetch('addMsg');
    }

    public function map()
    {
        $city_name = input('city_name');
        if(empty($city_name)){
            $city_id = session('city_id');
            $city_name = (new City())->getName($city_id);
        }
        $this->view->engine->layout(true);
        $this->assign('city_name', $city_name);
        return $this->fetch('map');
    }

    public function selectCity()
    {
        $city_id = input("post.city_id");
        session('city_id',$city_id);
        echo json_encode(['code'=>1,"msg"=>'请刷新网页']);exit;
    }
}
